import Vue from 'vue';
import {Message} from 'element-ui'
import CryptoJS from "crypto-js" 
Vue.component(Message);
const util = {
    getLastPath: function(path){
        let paths = path.split("/");
        return paths[paths.length - 1];
    },
    getUserSession: function(){
        return JSON.parse(sessionStorage.getItem("user"));
    },
    //设置cookie
    setCookie: function(c_name, c_pwd, exdays){
        var exdate = new Date(); //获取时间
        exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
        //字符串拼接cookie
        var username = CryptoJS.AES.encrypt(c_name, 'secret key 123').toString();
        var password = CryptoJS.AES.encrypt(c_pwd, 'secret key 123').toString();
        window.document.cookie = "userName" + "=" + username + ";path=/;expires=" + exdate.toGMTString();
        window.document.cookie = "userPwd" + "=" + password + ";path=/;expires=" + exdate.toGMTString();
    },
    //读取cookie
    getCookie: function(){
        var userobj = {};
        if (document.cookie.length > 0) {
            var arr = document.cookie.split('; '); //这里显示的格式需要切割
            for (var i = 0; i < arr.length; i++) {
                var arr2 = arr[i].split('='); 
                //判断查找相对应的值
                if (arr2[0] == 'userName') {
                    var bytes = CryptoJS.AES.decrypt(arr2[1], "secret key 123");
                    userobj.username = bytes.toString(CryptoJS.enc.Utf8);
                } else if (arr2[0] == 'userPwd') {
                    var bytes = CryptoJS.AES.decrypt(arr2[1], "secret key 123");
                    userobj.password = bytes.toString(CryptoJS.enc.Utf8);
                }
            }
        }
        return userobj;
    },
    //清除cookie
    clearCookie: function(){
        this.setCookie("", "", -1); //修改2值都为空，天数为负1天就好了
    },
    getErrorInfo: function(error_code){
        let error_info = "";
        switch(error_code.toString()){
            case "6000": error_info = "操作成功"; break;
            case "9000": error_info = "操作失败"; break;
            case "8000": error_info = "请登录"; break;
            case "9100": error_info = "注册错误"; break;
            case "9101": error_info = "无操作权限"; break;
            case "9102": error_info = "不合理的用户名"; break;
            case "9103": error_info = "用户名重复"; break;
            case "9104": error_info = "非法密码"; break;
            case "9200": error_info = "登录错误"; break;
            case "9201": error_info = "无操作权限"; break;
            case "9202": error_info = "用户不存在"; break;
            case "9203": error_info = "密码错误"; break;
            case "9204": error_info = "尝试次数过多，休息一会儿吧"; break;
            case "9205": error_info = "请刷新页面"; break;
            case "9301": error_info = "无操作权限"; break;
            case "9302": error_info = "请勿重复添加"; break;
            case "9303": error_info = "添加失败，没有足够的资源"; break;
            case "9304": error_info = "后台服务调用失败，请稍后重试"; break;
            case "9400": error_info = "删除错误"; break;
            case "9401": error_info = "无操作权限"; break;
            case "9402": error_info = "删除资源正在被使用"; break;
            case "9403": error_info = "删除资源不存在"; break;
            case "9404": error_info = "后台服务调用失败，请稍后重试"; break;
            case "9500": error_info = "更新错误"; break;
            case "9501": error_info = "无操作权限"; break;
            case "9502": error_info = "更新后的值不合理"; break;
            case "9503": error_info = "修改资源不存在"; break;
            case "9504": error_info = "修改内容与修改之前没有变化"; break;
            case "9601": error_info = "无操作权限"; break;
            case "9602": error_info = "设备资源不可用"; break;
            case "9701": error_info = "参数错误"; break;
            default: error_info = "不明原因"; break;
        }
        return error_info;
    },
    getIconClass: function(name){
        let icon_name = "";
        switch(name){
            case "车辆检测": icon_name = "icon-third-cheliangguanli"; break;
            case "雨雪雾检测": icon_name = "icon-third-yu"; break;
            case "停车": icon_name = "icon-third-cheliangguanli"; break;
            case "行人入侵": icon_name = "icon-third-xingren"; break;
            case "交通拥堵": icon_name = "icon-third-daoluyongdu"; break;
            default: icon_name = "icon-third-cheliangguanli"; break;
        }
        return icon_name;
    },
    tip: function(msg){    
        Message({        
            message: msg,
            type: 'error' 
        });
    },
    hasFlash(){
        let hasFlash = false;
        try {
            hasFlash = Boolean(new ActiveXObject('ShockwaveFlash.ShockwaveFlash'));
        } catch(exception) {
            hasFlash = ('undefined' != typeof navigator.mimeTypes['application/x-shockwave-flash']);
        }
        return hasFlash;
    },
    formatDate(date, fmt) {
        if (/(y+)/.test(fmt)) {
            fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length));
        }
        let o = {
            'M+': date.getMonth() + 1,
            'd+': date.getDate(),
            'h+': date.getHours(),
            'm+': date.getMinutes(),
            's+': date.getSeconds()
        };
        for (let k in o) {
            if (new RegExp(`(${k})`).test(fmt)) {
                let str = o[k] + '';
                fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? str : this.padLeftZero(str));
            }
        }
        return fmt;
    },
    padLeftZero(str) {
        return ('00' + str).substr(str.length);
    }
}

export default util;